home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Snippets / Networking / AppleTalk Libraries / NBP.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-03  |  5.2 KB  |  174 lines  |  [TEXT/MPS ]

  1. /*------------------------------------------------------------------------------
  2. #
  3. #    NBP.c : this module contains all the Name Binding Protocol functions
  4. #
  5. #
  6. #    Versions:    1.0                    10/91
  7. #    C.Buttin - Apple Computer Europe            
  8. #
  9. ------------------------------------------------------------------------------*/
  10. #include <Limits.h>
  11. #include <errors.h>
  12. #include <Types.h>
  13. #include <Memory.h>
  14. #include <stdIO.h>
  15. #include <String.h>
  16. #include <Strings.h>
  17. #include <Devices.h>
  18. #include <AppleTalk.h>
  19.  
  20.  
  21. /* functions */
  22. pascal    Boolean     InitNBP(Boolean SelfOn);        /* SelfSend mode */
  23. pascal    void         CloseNBP(Boolean SelfOn);        /* SelfSend mode */
  24. /* SetSelfSend :  Set self mode */
  25. pascal    void         SetSelfSend(Boolean flagOn);
  26.  
  27. /* NBPGetList : Get the List of entities with a specific entity name into a buffer
  28.    Input : 
  29.            - Ptr on a buffer to receive the data
  30.         - size of the buffer
  31.         - pointer on the names of entities to be looked for 
  32.         - maximum number of entities to be looked for
  33.     Output :
  34.         - total number of entities or an error code */ 
  35. pascal    short         NBPGetList(Ptr buffer,
  36.                                short bufSize,
  37.                                EntityPtr entity,
  38.                                short maxEntities);
  39.  
  40. /* NBPGetAddress : Extract the AppleTalk address of an entity
  41.    Input : 
  42.            - Ptr on a buffer containing the data
  43.         - position of the AppleTalk address to be found
  44.         - total numver of entities 
  45.         - NBP name of the entity
  46.         - AppleTalk address of the entity
  47.     Output :
  48.         - true if the address is found */ 
  49. pascal    Boolean     NBPGetAddress(Ptr buffer,
  50.                                   short tuplenum,
  51.                                   short numEntities,
  52.                                   EntityPtr entity,
  53.                                   AddrBlock *address);
  54.  
  55. /* NBPRegisterEntity : creates an entry in the Name Binding Table
  56.     Input : 
  57.           - socket number
  58.         - entity to be associated with this socket
  59.     OutPut :
  60.         - Pointer on the name table entry. This pointer must be available as
  61.           long as the entity has not been removed from the table. */ 
  62. pascal    Ptr         NBPRegisterEntity(unsigned char theSocket,
  63.                                  EntityPtr entity);
  64.  
  65. /* NBPRemoveEntity : remove an entity from the Name Binding Table
  66.     Input : 
  67.           - entity    */
  68. pascal    OSErr         NBPRemoveEntity(EntityPtr entity);
  69.  
  70.  
  71.  
  72.                             /**     CODE        **/
  73.                             
  74.  
  75. pascal Boolean InitNBP(Boolean SelfOn)
  76. {
  77.  
  78.     if (MPPOpen() != noErr)
  79.         return false;
  80.     if (SelfOn)
  81.         SetSelfSend(true);                    
  82.  
  83. } /* InitNBP */
  84.  
  85.  
  86. pascal void CloseNBP(Boolean SelfOn)
  87. {
  88.     SetSelfSend(SelfOn);        /* disable SelfSend for other applications (chooser...) */
  89. } /* CloseNBP */
  90.  
  91. /* Set self mode */
  92. pascal void SetSelfSend(Boolean flagOn)        /* true to enabled it, false to disable it */
  93. {
  94.     MPPParamBlock MPPBlock;
  95.     
  96.     MPPBlock.SETSELF.newSelfFlag = flagOn;           /* self-send toggle flag */
  97.     PSetSelfSend((MPPPBPtr)&MPPBlock,false);
  98.  
  99. } /* SetSelfSend */
  100.  
  101. /* Get the names and addresses of the entities defined by a NBP name.
  102.    Return the items in the buffer */
  103. pascal short NBPGetList(Ptr buffer,short bufSize,EntityPtr entity,short maxEntities)
  104. {    
  105.     MPPParamBlock     MPPBlock;            /* to build parameter block for NBP */
  106.     OSErr            error;
  107.     
  108.     if (bufSize < (sizeof(EntityName)+sizeof(AddrBlock)) * maxEntities)
  109.         return(paramErr);
  110.     /* set NBP record */
  111.     MPPBlock.NBP.ioCompletion = nil;                /* no completion routine */
  112.     MPPBlock.NBP.interval = 8;                          /* retry interval(8-ticks units) */
  113.     MPPBlock.NBP.count = 3;                            /* retry count */
  114.     MPPBlock.NBP.NBPPtrs.entityPtr = (Ptr)entity;    /* entity name to be looked at */
  115.     MPPBlock.NBP.parm.Lookup.retBuffPtr = buffer;     /* reception buffer */
  116.     MPPBlock.NBP.parm.Lookup.retBuffSize = bufSize;
  117.     MPPBlock.NBP.parm.Lookup.maxToGet  = maxEntities;
  118.             
  119.     if ((error = PLookupName((MPPPBPtr) &MPPBlock,false)) != noErr) 
  120.         return error;
  121.  
  122.     /* Return the actual number of entities */
  123.     return(MPPBlock.NBP.parm.Lookup.numGotten);
  124. } /* NBPGetList */
  125.  
  126. /* Get the AppleTalk address of the nth entity  */
  127. pascal Boolean NBPGetAddress(Ptr buffer,short tuplenum,short numEntities,EntityPtr entity,AddrBlock *address)
  128. {
  129.     
  130.     if (tuplenum > numEntities)
  131.         return false;
  132.     if (NBPExtract(buffer,numEntities,tuplenum,entity, address) != noErr) {
  133.         return false;
  134.         }
  135.     else return true;
  136. } /* NBPGetAddress */
  137.  
  138. /* register an an entity in the Name Binding Table. */
  139. pascal Ptr NBPRegisterEntity(unsigned char theSocket,EntityPtr entity)
  140. {
  141.     MPPParamBlock     MPPBlock;    
  142.     Ptr                NTEPtr;
  143.     
  144.     /* Builds up entity name  */
  145.     if (!(NTEPtr = NewPtr(sizeof(EntityName)+9)))
  146.         return nil;
  147.     NBPSetNTE(NTEPtr,entity->objStr,entity->typeStr,entity->zoneStr,theSocket); 
  148.  
  149.     /* set NBP record */
  150.     MPPBlock.NBP.ioCompletion = nil;        /* no completion routine */
  151.     MPPBlock.NBP.interval = 8;              /* retry interval (8-tick unit) */
  152.     MPPBlock.NBP.count = 1;                 /* retry count */
  153.     MPPBlock.NBP.NBPPtrs.ntQElPtr = NTEPtr;    /* entity name + socket to be registered at */
  154.     MPPBlock.NBP.parm.verifyFlag = 1;        /* verify if name already exists */
  155.     if (PRegisterName((MPPPBPtr)&MPPBlock,false) != noErr) {
  156.         DisposPtr(NTEPtr);
  157.         return nil;
  158.         }
  159.     else return NTEPtr;
  160. } /* NBPRegisterEntity */
  161.  
  162. /* remove an entity from the binding table */
  163. pascal OSErr NBPRemoveEntity(EntityPtr entity)            
  164. {
  165.     MPPParamBlock MPPBlock;        
  166.  
  167.     /* set up NBP ParamBlock */
  168.     MPPBlock.NBP.ioCompletion = nil;                /* no completion routine */
  169.     MPPBlock.NBP.NBPPtrs.entityPtr = (Ptr)entity;    /* entity name to be removed */
  170.  
  171.     return (PRemoveName((MPPPBPtr)&MPPBlock,false));
  172. } /* NBPRemoveEntity */
  173.  
  174.